package com.cmlite;

import org.json.JSONException;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.cmlite.base.BaseActivity;
import com.cmlite.base.BaseInit;
import com.cmlite.component.ClearableEditText;
import com.cmlite.connect.AsyncTask_Api;
import com.cmlite.connect.AsyncTask_ApiListener;
import com.cmlite.connect.CallApi;
import com.cmlite.connect.ScreenStopper;
import com.cmlite.model.OutResult;
import com.cmlite.model.PeopleModel;
import com.cmlite.util.ConnectionException;
import com.cmlite.util.EmptyDataException;
import com.cmlite.util.SharedPreferencesDB;
import com.cmlite.util.TokenUnableException;
import com.cmlite.util.Util;

public class A005AddPeople extends BaseActivity implements BaseInit,
		OnClickListener, AsyncTask_ApiListener {
	public static final int RESULT_A004 = 0;
	private static final int TYPE_ADD_PEOPLE = 13;
	private static final int TYPE_EDIT_PEOPLE = 15;

	private static final int ADD_PEOPLE = 1;
	private static final int EDIT_PEOPLE = 2;

	private TextView btn_back, btn_register, tv_title;
	private TextView[] tv_labels;
	private ClearableEditText et_firstname, et_lastname, et_field_01,
			et_field_02, et_field_03;
	private String labels[];
	private String keys[];
	private String values[];
	private int type;
	private String mEventName;
	private PeopleModel mPeople;
	private String mEventId;

	@Override
	public void onIOnCreate(Bundle savedInstanceState) {
		initView();
	}

	@Override
	public void onIRestart() {

	}

	@Override
	public void onIStart() {

	}

	@Override
	public void onIResume() {

	}

	@Override
	public void onIDestroy() {

	}

	@Override
	public void onIActivityResult(int requestCode, int resultCode, Intent data) {

	}

	@Override
	public void onIBackPressed() {

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.header_tv_back: // back
			Intent i = new Intent(mContext, A004ListPeople.class);
			setResult(RESULT_A004, i);
			finish();
			break;
		case R.id.header_tv_next: // done
			fSave();
			break;
		default:
			break;
		}
	}

	@Override
	public Object callApi(int serachType, Object apidata) {
		if (type == ADD_PEOPLE) {
			try {
				SharedPreferencesDB db = new SharedPreferencesDB(this);

				return CallApi.addPeople(mContext, db.getPrefToken(), mEventId,
						keys, values);
			} catch (EmptyDataException e) {
				e.printStackTrace();
			} catch (ConnectionException e) {
				e.printStackTrace();
			} catch (JSONException e) {
				e.printStackTrace();
			} catch (TokenUnableException e) {
				e.printStackTrace();
			}
		} else if (type == EDIT_PEOPLE) {
			try {
				SharedPreferencesDB db = new SharedPreferencesDB(this);
				// return CallApi.editPeople(mContext, db.getPrefToken(),
				// mEventId, mPeople.getmId(), keys, values);
				return CallApi.editPeople(mContext, db.getPrefToken(),
						mEventId, mPeople.getmPeople_ID(), keys, values);
			} catch (EmptyDataException e) {
				e.printStackTrace();
			} catch (ConnectionException e) {
				e.printStackTrace();
			} catch (JSONException e) {
				e.printStackTrace();
			} catch (TokenUnableException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public void finishApi(int serachType, Object apiresult) {
		OutResult out = (OutResult) apiresult;
		if (out.getStatus() && out.getCode() == 200) {
			if (type == ADD_PEOPLE) {
				if (out.getmPeople() != null) {
					mPeople.setmPeople_ID(out.getmPeople().getmPeople_ID());
				}
				backListPeople();
			} else if (type == EDIT_PEOPLE) {
				// this.finish();
				backListPeople();
			}
		}

	}

	private void backListPeople() {
		Intent i = new Intent(mContext, A004ListPeople.class);
		i.putExtra("type", type);
		i.putExtra("params", mPeople.getPeopleArray());
		setResult(RESULT_A004, i);
		finish();
	}

	@Override
	public void tokenExpride() {

	}

	@Override
	public void initView() {
		// TODO Auto-generated method stub
		setContentView(R.layout.a005addpeople);

		tv_title = (TextView) findViewById(R.id.header_tv_title);
		btn_back = (TextView) findViewById(R.id.header_tv_back);
		btn_register = (TextView) findViewById(R.id.header_tv_next);
		tv_labels = new TextView[5];
		tv_labels[0] = (TextView) findViewById(R.id.label1);
		tv_labels[1] = (TextView) findViewById(R.id.label2);
		tv_labels[2] = (TextView) findViewById(R.id.label3);
		tv_labels[3] = (TextView) findViewById(R.id.label4);
		tv_labels[4] = (TextView) findViewById(R.id.label5);

		btn_back.setVisibility(View.VISIBLE);
		btn_register.setVisibility(View.VISIBLE);

		et_firstname = (ClearableEditText) findViewById(R.id.a005_et_firstname);
		et_lastname = (ClearableEditText) findViewById(R.id.a005_et_lastname);
		et_field_01 = (ClearableEditText) findViewById(R.id.a005_et_field_01);
		et_field_02 = (ClearableEditText) findViewById(R.id.a005_et_field_02);
		et_field_03 = (ClearableEditText) findViewById(R.id.a005_et_field_03);

		btn_back.setOnClickListener(this);
		btn_register.setOnClickListener(this);

		initData();
	}

	@Override
	public void initData() {
		// TODO Auto-generated method stub
		btn_register.setBackgroundResource(R.drawable.finished_work);

		mPeople = new PeopleModel();
		Bundle b = getIntent().getExtras();
		type = b.getInt("type");
		labels = b.getStringArray("labels");
		keys = b.getStringArray("keys");
		mEventName = b.getString("event_name");
		mEventId = b.getString("event_id");
		PeopleModel people = (PeopleModel) getIntent().getSerializableExtra(
				"people");
		setTextLabels(labels);
		et_firstname.setHint(labels[0]);
		et_lastname .setHint(labels[1]);
		et_field_01 .setHint(labels[2]);
		et_field_02 .setHint(labels[3]);
		et_field_03 .setHint(labels[4]);
		 if (type == EDIT_PEOPLE) {
			mPeople = people;
			et_firstname.setText(mPeople.getmField_01());
			et_lastname.setText(mPeople.getmField_02());
			et_field_01.setText(mPeople.getmField_03());
			et_field_02.setText(mPeople.getmField_04());
			et_field_03.setText(mPeople.getmField_05());
		}
	}

	private void setTextLabels(String[] labels) {
		this.btn_back.setText(mEventName);

		for (int i = 0; i < labels.length; ++i) {
			tv_labels[i].setText(labels[i]);
		}
	}

	private void fSave() {
		try {
			values = new String[labels.length];
			values[0] = et_firstname.getText().toString().trim();
			values[1] = et_lastname.getText().toString().trim();
			values[2] = et_field_01.getText().toString().trim();
			values[3] = et_field_02.getText().toString().trim();
			values[4] = et_field_03.getText().toString().trim();

			if (Util.isEmpty(values[0])) {
				Util.showDialog(mContext, R.string.title_register,
						R.string.error_firstname, R.drawable.exclamation);
			} else if (Util.isEmpty(values[1])) {
				Util.showDialog(mContext, R.string.title_register,
						R.string.error_lastname, R.drawable.exclamation);
			} else { // if no error
				updatePeople();
				if (type == ADD_PEOPLE) {
					if (Util.getNetworkStatus(mContext)) {
						AsyncTask_Api apiTask = new AsyncTask_Api(this, this,
								TYPE_ADD_PEOPLE, 0,
								ScreenStopper.STYLE_PROGRESS_PROCESS);
						apiTask.execute();
					} else {
						Util.showDialog(this, R.string.warning,
								R.string.network_not_available,
								R.drawable.exclamation);
					}
				} else if (type == EDIT_PEOPLE) {
					if (Util.getNetworkStatus(mContext)) {
						AsyncTask_Api apiTask = new AsyncTask_Api(this, this,
								TYPE_EDIT_PEOPLE, 0,
								ScreenStopper.STYLE_PROGRESS_PROCESS);
						apiTask.execute();
					} else {
						Util.showDialog(this, R.string.warning,
								R.string.network_not_available,
								R.drawable.exclamation);
					}
				}

			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

	}

	private void updatePeople() {
		mPeople.setmField_01(values[0]);
		mPeople.setmField_02(values[1]);
		mPeople.setmField_03(values[2]);
		mPeople.setmField_04(values[3]);
		mPeople.setmField_05(values[4]);
	}
}
